package test.service;

import org.json.JSONArray;
import test.testcase.TestJob;
import test.testcase.dao.JobDao;
import test.utils.JsonResult;

public class ServiceJob {
	
	private static TestJob setJob(JsonResult result) {
		JSONArray array = result.read("data").toJsonArray();
		if (array != null && array.length() > 0) {
			TestJob testJob = new TestJob();
			testJob.setCreatetime(result.read("data[0].createtime").toString());
			testJob.setUserid(result.read("data[0].userid").toString());
			testJob.setId(result.read("data[0].id").toString());
			testJob.setTitle(result.read("data[0].title").toString());
			testJob.setStatus(result.read("data[0].status").toString());
			testJob.setDesc(result.read("data[0].desc").toString());
			testJob.setUpdatetime(result.read("data[0].updatetime").toString()); 
			testJob.setExcuteid(result.read("data[0].excuteid").toString());
			testJob.setProjectid(result.read("data[0].projectid").toString());
			return testJob;
		}
		return null;
	}
	
	public static TestJob getAnExecutingJob(String projectid) {
		JsonResult result = JobDao.getAnExecutingJob(projectid);
		return setJob(result);
	}
	
	public static TestJob getAnExecutableJob(String projectid) {
		JsonResult result = JobDao.getAnExecutableJob(projectid);
		return setJob(result);
	}
	
	public static TestJob getAnExecutableJob() {
		JsonResult result = JobDao.getAnExecutableJob();
		return setJob(result);
	}
	
	public static TestJob getAnPenddingJob() {
		JsonResult result = JobDao.getAnPenddingJob();
		return setJob(result);
	}
	
	public static TestJob getJob(String jobid) {
		JsonResult result = JobDao.getJob(jobid);
		JSONArray array = result.read("data").toJsonArray();
		TestJob testJob = new TestJob();
		if (array != null) {
			testJob.setCreatetime(result.read("data[0].createtime").toString());
			testJob.setUserid(result.read("data[0].userid").toString());
			testJob.setUserid(result.read("data[0].userid").toString());
			testJob.setId(result.read("data[0].id").toString());
			testJob.setTitle(result.read("data[0].title").toString());
			testJob.setStatus(result.read("data[0].status").toString());
			testJob.setDesc(result.read("data[0].desc").toString());
			testJob.setUpdatetime(result.read("data[0].updatetime").toString()); 
			testJob.setExcuteid(result.read("data[0].excuteid").toString());
			testJob.setProjectid(result.read("data[0].projectid").toString());
		}
		return testJob;
	}

}
